package com.sys.app.web.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sys.app.entity.result.ResultDTO;
import com.sys.app.entity.sys.SysAccount;
import com.sys.app.entity.sys.SysMenu;
import com.sys.app.entity.sys.TreeNode;
import com.sys.app.entity.sys.myConstants;
import com.sys.app.entity.token.BaseUserInfo;
import com.sys.app.entity.token.TokenUtil;
import com.sys.app.entity.util.StringUtil;
import com.sys.app.entity.util.TreeUtil;
import com.sys.app.service.sys.ISysMenuService;
import com.sys.app.web.base.TokenInterceptor;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("main")
public class MainContorller {
    Logger logger = LoggerFactory.getLogger(MainContorller.class);
    private ISysMenuService iSysMenuService;

    public MainContorller(ISysMenuService iSysMenuService){
        this.iSysMenuService=iSysMenuService;
    }

    /**
     * 主页首页
     * @return
     */
    @GetMapping("/index.do")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("main/index");
        modelAndView.addObject("name", BaseUserInfo.getUser().getName()+",欢迎您!");
        return modelAndView;
    }

    @GetMapping("/getMenu.do")
    @ResponseBody
    public ResultDTO getMenu() {
        String json="[]";
        QueryWrapper<SysMenu> queryWrapper=new QueryWrapper<SysMenu>();
        queryWrapper.eq("STATUS",0);
        queryWrapper.orderByAsc("MENU_ORDER");
        List<SysMenu> menuList=iSysMenuService.list(queryWrapper);
        List<TreeNode> treeNodeList = new ArrayList<>();
        if(menuList!=null&&menuList.size()>0){
            for (SysMenu item:menuList){
                if(StringUtils.isNotBlank(item.getMenuUrl())){
                    item.setMenuUrl(item.getMenuUrl());
                }
                treeNodeList.add(new TreeNode(item.getId(),item.getPid(),item.getName(), item.getMenuUrl(),item.getIconCls()));
            }
        }
        if(treeNodeList!=null&&treeNodeList.size()>0) {
            List<TreeNode> treeNodes = TreeUtil.buildTreeTwo(treeNodeList);
            json= JSON.toJSONString(treeNodes).replace("[]","null");
            return ResultDTO.ok().result(json);
        }else{
            return ResultDTO.error().result("[]");
        }

    }

}
